package cn.com.cifi.mars.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.cifi.mars.bean.dto.NOSRoomCompensationDto;
import cn.com.cifi.mars.bean.dto.NosTradedataDto;
import cn.com.cifi.mars.constant.Constants;
import cn.com.cifi.mars.entity.DmOdsNosTradedataviewformars;
import cn.com.cifi.mars.entity.NosViewTradedataviewformars;
import cn.com.cifi.mars.mapper.mars.DmOdsNosTradedataviewformarsMapper;
import cn.com.cifi.mars.service.DmOdsNosTradedataviewformarsService;
import cn.com.cifi.mars.service.NosViewTradedataviewformarsService;
import cn.com.cifi.mars.util.ListUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author plx
 * @since 2019-12-17
 */
@Service
public class DmOdsNosTradedataviewformarsServiceImpl extends ServiceImpl<DmOdsNosTradedataviewformarsMapper, DmOdsNosTradedataviewformars> implements DmOdsNosTradedataviewformarsService {

    private static final Logger logger = LoggerFactory.getLogger(DmOdsNosTradedataviewformarsServiceImpl.class);
    @Autowired
    private NosViewTradedataviewformarsService nosViewTradedataviewformarsService;
    @Autowired
    private DmOdsNosTradedataviewformarsMapper dmOdsNosTradedataviewformarsMapper;

    @Override
    public void syncNosContact() {
        //清空
        remove(new QueryWrapper<>());
        logger.info("清空DmOdsNosTradedataviewformars数据完毕");
        //同步
        logger.info("同步DmOdsNosTradedataviewformars数据开始");
        int batch = 5000;
        List<NosViewTradedataviewformars> list = nosViewTradedataviewformarsService.list();
        List<List> tempList = ListUtils.getSubList(list, batch);
        LocalDateTime now = LocalDateTime.now();
        int i = 0;
		for (List<NosViewTradedataviewformars> _list : tempList) {
			i++;
			List<NosViewTradedataviewformars> subList = (List<NosViewTradedataviewformars>) _list;
            List<DmOdsNosTradedataviewformars> tmp = subList.parallelStream()
                    .map(x -> {
                        DmOdsNosTradedataviewformars dest = new DmOdsNosTradedataviewformars();
                        dest.setEtlCreateTime(now);
                        dest.setEtlUpdateTime(now);
                        try {
                            PropertyUtils.copyProperties(dest, x);
                        } catch (Exception e) {
                            logger.error(e.getMessage(), e);
                        }
                        return dest;
                    }).collect(Collectors.toList());
            saveBatch(tmp);
        }
        logger.info("同步DmOdsNosTradedataviewformars数据结束");
    }

    @Override
    public List<NosTradedataDto> selectTradeRooms(String projectFid) {
        return dmOdsNosTradedataviewformarsMapper.selectTradeRooms(projectFid, Constants.NOS_TRADE);
    }

    @Override
    public List<NosTradedataDto> selectCheckRooms(String projectFid) {
        return dmOdsNosTradedataviewformarsMapper.selectTradeRooms(projectFid, Constants.NOS_TUIFANG);
    }

    @Override
    public List<NOSRoomCompensationDto> selectTradeCompensation(String projectFid) {
        return dmOdsNosTradedataviewformarsMapper.selectTradeCompensation(projectFid, Constants.NOS_BUCHA);
    }

    @Override
    public List<NosTradedataDto> selectNosContractList(List<String> buildings) {
        return dmOdsNosTradedataviewformarsMapper.selectNosContractList(buildings);
    }


}
